package com.zhangh.summer.admin.support.spring;

import com.zhangh.summer.admin.support.boot.common.AppConfigEnum;
import com.zhangh.summer.admin.support.boot.interfaces.OnAppConfigConditional;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @description: TODO
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2020-05-08 18:02
 **/
@Configuration
@OnAppConfigConditional(AppConfigEnum.CORS_ENABLE)
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("*");
        config.setAllowCredentials(true);
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        return new CorsFilter(configSource);
    }
}
